package com.gather.arraylists;

import com.gather.orm.Book;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;

// 练习题
public class Example04 {
    public static void main(String[] args) {
        System.out.println("测试");
        // 使用List的实现类添加三本图书 并遍历打印
        // 1.按价格排序，低到高
        // 2.要求使用ArrayList\LinkedList Vector 三种集合实现
        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("红色的花","罗贯中",50.3));
        books.add(new Book("绿色的雨季","吴承恩",20.5));
        books.add(new Book("橙色的城市","曹雪芹",10.2));
        books.add(new Book("开心的颜色","施耐庵",34.6));
        //System.out.println("book "+books);

        // 方式一：
        //for (int i = 0; i < books.size(); i++) {
        //    // 循环比较
        //    for (int j = i; j < books.size(); j++) {
        //        double price = books.get(i).getPrice();
        //        Book book = books.get(i);
        //        double price2 = books.get(j).getPrice();
        //        Book book2 = books.get(j);
        //        if (price > price2) {
        //            // 互换位置
        //            books.set(i,book2);
        //            books.set(j,book);
        //        }
        //    }
        //}
        System.out.println("======");
        // 方式二
        for (int i = 0; i < books.size() - 1; i++) {
            for (int j = 0; j < books.size() - 1 -i; j++) {
                Book book = books.get(j);
                Book book2 = books.get(j+1);
                if (book.getPrice() > book2.getPrice()) {
                    books.set(j,book2);
                    books.set(j + 1,book);
                }
            }
        }
        System.out.println("book "+books);
        // 按照价格排序
        //for (Book book : books) {
        //    double price = book.getPrice();
        //}
        // 冒泡算法
        int[] num = {23,12,56,42,78};
        System.out.println(Arrays.toString(num));
        for (int i = 0; i < num.length; i++) {
                // -1 表示最后一轮不用比较
            for (int j = i; j < num.length -1; j++) {
                int a = num[i];
                if (a > num[j]) {
                    // 互换位置
                    num[i] = num[j];
                    num[j] = a;
                }
            }
        }
        System.out.println(Arrays.toString(num));
    }
}
